package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.MyBankAdapter;
import com.gears_software.sxdc.dccx.data.finder.IBankFinder;
import com.gears_software.sxdc.dccx.data.finder.ICPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.UserBankCardInformation;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.gears_software.sxdc.dccx.ui.util.DES3;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.LongClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/8/7.
 * 银行卡列表界面
 */
@EActivity(R.layout.activity_bank_card)
public class BankCardActivity extends Activity implements MyBankAdapter.IBankClickListener {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @RestService
    IBankFinder bankFinder;
    @ViewById
    TextView tv_bank;
    @ViewById
    TextView tv_name;
    @ViewById
    TextView tv_bank_card_number;
    @ViewById
    SimpleDraweeView iv_bank_image;
    @ViewById
    TextView txt_nodata_icon;
    @ViewById
    RelativeLayout re_wu;
    @ViewById
    ListView mybank_list;
    UserBankCardInformation information;
    @RestService
    IMemberFinder memberFinder;
    MemberModel memberModel;

    String state = "";
    MaterialDialogProgress progress;
    MyBankAdapter adapter;


    @AfterViews
    void created() {
        actionbar_title.setText("银行卡");
        information = new UserBankCardInformation();
        initIconfont();
        StatusBarCompat.setStatusBarColor(this,ContextCompat.getColor(this, R.color.colorActionbar));

        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getUserBankCardInformation();
        getCurrentMember();
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setText(R.string.mybank_add_icon);
        right_icon.setTypeface(app.iconfont);
        right_icon.setTextSize(28);
        txt_nodata_icon.setTypeface(app.iconfont);
    }


    @Click
    void right_icon() {
        if (state.equals("4")) {
            if (information.getCardno() == null || information.getCardno().equals("")) {
                Intent intent = new Intent(this, AddBankCardActivity_.class);
                intent.putExtra("flag", "add");
                startActivityForResult(intent, 1);
            } else {
                new MaterialDialog.Builder(this)
                        .title("提示")
                        .titleGravity(GravityEnum.CENTER)
                        .titleColorRes(R.color.colorFontGray)
                        .content("你已绑定银行卡信息，请勿重复添加！")
                        .contentColorRes(R.color.black)
                        .positiveText("确定")
                        .positiveColor(ContextCompat.getColor(this, R.color.colorFontBlue))
                        .buttonsGravity(GravityEnum.CENTER)
                        .show();
            }
        } else {
            TastyToast.makeText(this, "请先进行实名认证", app.SHORT, app.WARNING);
        }
    }

    @Click
    void back_icon() {
        finish();
    }

    @Background
    void getUserBankCardInformation() {
        ResultModel<UserBankCardInformation> res = bankFinder.getUserBankCardInformation();
        showResout(res);
    }


    @UiThread
    void showResout(ResultModel<UserBankCardInformation> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    information = res.getData();
                    if (information.getBank() == null || information.getBankhead().equals("")) {
                        mybank_list.setVisibility(View.GONE);
                        re_wu.setVisibility(View.VISIBLE);
                    } else {
                        mybank_list.setVisibility(View.VISIBLE);
                        re_wu.setVisibility(View.GONE);
                        adapter = new MyBankAdapter(this, information);
                        adapter.setClickListener(this);
                        mybank_list.setAdapter(adapter);
                    }
                }
            }
        }
        progress.dismiss();
    }

    @Background
    void getCurrentMember() {
        ResultModel<MemberModel> result = memberFinder.getMember();
        showMemberResult(result);
    }

    @UiThread
    void showMemberResult(ResultModel<MemberModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    memberModel = res.getData();
                    state = memberModel.getPassengerauditstate();
                }
            }
        }
    }

    @Background
    void deleteBankCard() {
        ResultModel<String> result = bankFinder.deleteBankCard();
        show(result);
    }

    @UiThread
    void show(ResultModel<String> result) {
        if (result != null) {
            if (result.getErrno().equals("0")) {
                TastyToast.makeText(this, "删除银行卡成功", app.SHORT, app.SUCCESS);
                information = new UserBankCardInformation();
                getUserBankCardInformation();
            } else {
                TastyToast.makeText(this, "删除银行卡失败", app.SHORT, app.ERROR);
            }
        }
    }

    private void showDeleteDialog(String title) {
        new MaterialDialog.Builder(this)
                .title("提示")
                .titleGravity(GravityEnum.CENTER)
                .titleColorRes(R.color.colorFontGray)
                .content(title)
                .contentColorRes(R.color.black)
                .positiveText("确定")
                .positiveColor(ContextCompat.getColor(this, R.color.colorFontBlue))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        deleteBankCard();
                    }
                })
                .negativeText("取消")
                .negativeColor(ContextCompat.getColor(this, R.color.colorFontGray))
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                })
                .buttonsGravity(GravityEnum.CENTER)
                .show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            getUserBankCardInformation();
        }
    }

    @Override
    public void onClick(String flag) {
        if (flag != null && !flag.equals("")) {
            // flag 1 表示编辑
            if (flag.equals("1")) {
                Intent intent = new Intent(BankCardActivity.this, AddBankCardActivity_.class);
                Bundle bundle = new Bundle();
                bundle.putString("flag", "bj");
                bundle.putString("name", information.getBankhead());
                bundle.putString("bank", information.getBank());
                bundle.putString("cardno", information.getCardno());
                bundle.putString("account", information.getBankaccount());
                bundle.putString("bankicon",information.getBankicon());
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
            } else if (flag.equals("2")) { // flag 2 表示删除
                showDeleteDialog("您确定删除银行卡信息吗？");
            }
        }
    }
}



